package com.tuyue.police.biz.impl;

import com.tuyue.police.biz.ISectorBiz;
import com.tuyue.police.dao.IEnterpriseDao;
import com.tuyue.police.dao.IRule1Dao;
import com.tuyue.police.dao.ISectorDao;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.Enterprise;
import com.tuyue.police.pojo.Rule1;
import com.tuyue.police.pojo.Sector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/27.
 * @Modified By:
 */
@Service
public class SectorBizImpl implements ISectorBiz {

    @Autowired
    private ISectorDao sectorDao;

    @Autowired
    private IEnterpriseDao enterpriseDao;

    @Autowired
    private IRule1Dao rule1Dao;


    @Override
    public List<Sector> settingSector(Sector[] sectors) {
        ArrayList<Sector> list = new ArrayList<>();
        for (Sector sector : sectors) {
            Sector sectorDaoOne = sectorDao.findOne(sector.getSectorId());
            if (ObjectUtils.isEmpty(sectorDaoOne)) {
                throw new MyException(ResultEnum.SECTOR_ERROR);
            }
            sectorDaoOne.setSectorX(sector.getSectorX());
            sectorDaoOne.setSectorY(sector.getSectorY());
            list.add(sectorDaoOne);
        }


        List<Sector> save = sectorDao.save(list);
        return save;
    }

    @Override
    public List<Sector> findListSector(Long eId) {
        Enterprise one = enterpriseDao.findOne(eId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.ENTERPRISE_ERROR);
        }
        List<Sector> list = sectorDao.findAllByEnterprise(eId);
        return list;
    }

    @Override
    public void delSector(List<Long> sIda) {
        List<Sector> sectors = new ArrayList<>();
        sIda.forEach(sId ->{
            Sector sector = sectorDao.getOne(sId);
            if (ObjectUtils.isEmpty(sector)) {
                throw new MyException(ResultEnum.SECTOR_ERROR);
            }
            List<Rule1> rule1List = rule1Dao.findAllByRule1SectorId(sId);
            if (!ObjectUtils.isEmpty(rule1List)) {
                throw new MyException(ResultEnum.SECTOR_DELERROR);
            }
            sectors.add(sector);
        });


        sectorDao.delete(sectors);

    }
}
